/**
 * Created by JetBrains WebStorm.
 * User: denysk
 * Date: 7/25/11
 * Time: 5:41 PM
 * To change this template use File | Settings | File Templates.
 */

Namespace("sketch.framework");

/**
 * Draws a rounded rectangle using the current state of the canvas.
 * If you omit the last three params, it will draw a rectangle
 * outline with a 5 pixel border radius
 * @param {Number} x The top left x coordinate
 * @param {Number} y The top left y coordinate
 * @param {Number} width The width of the rectangle
 * @param {Number} height The height of the rectangle
 * @param {Number} radius The corner radius. Defaults to 5;
 * @param {FillStyle} fillStyle Whether to fill the rectangle.
 * @param {StrokeStyle} stroke Whether to stroke the rectangle. Defaults to true.
 */

(function () {
    var fwk = sketch.framework;

    fwk.initializeContextExtensions = function() {
        CanvasRenderingContext2D.prototype.roundedRect = function (x, y, width, height, radius, fillStyle, strokeStyle) {
            if (typeof radius === undefined) {
                radius = 5;
            }

            this.save();

            if (fillStyle !== undefined) {
                this.fillStyle = fillStyle;
            }
            if (strokeStyle !== undefined) {
                this.strokeStyle = strokeStyle;
                this.lineWidth = 2;
            }

            this.beginPath();
            this.moveTo(x + radius, y);
            this.lineTo(x + width - radius, y);
            this.quadraticCurveTo(x + width, y, x + width, y + radius);
            this.lineTo(x + width, y + height - radius);
            this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
            this.lineTo(x + radius, y + height);
            this.quadraticCurveTo(x, y + height, x, y + height - radius);
            this.lineTo(x, y + radius);
            this.quadraticCurveTo(x, y, x + radius, y);
            this.closePath();

            if (strokeStyle !== undefined) {
                this.stroke();
            }
            if (fillStyle !== undefined) {
                this.fill();
            }

            this.restore();
        };

        CanvasRenderingContext2D.prototype.roundedRectPath = function (x, y, width, height, rx, ry) {
            this.beginPath();
            this.moveTo(x + rx, y);
            this.lineTo(x + width - rx, y);
            this.quadraticCurveTo(x + width, y, x + width, y + ry);
            this.lineTo(x + width, y + height - ry);
            this.quadraticCurveTo(x + width, y + height, x + width - rx, y + height);
            this.lineTo(x + rx, y + height);
            this.quadraticCurveTo(x, y + height, x, y + height - ry);
            this.lineTo(x, y + ry);
            this.quadraticCurveTo(x, y, x + rx, y);
            this.closePath();
        };

        CanvasRenderingContext2D.prototype.rectPath = function (x, y, width, height) {
            this.beginPath();
            this.moveTo(x, y);
            this.lineTo(x + width, y);
            this.lineTo(x + width, y + height);
            this.lineTo(x, y + height);
            this.lineTo(x, y);
            this.closePath();
        };

        CanvasRenderingContext2D.prototype.sideRoundedRectPath = function (x, y, width, height, leftRadius, rightRadius) {

            this.beginPath();

            this.moveTo(x + leftRadius, y);

            this.lineTo(x + width - rightRadius, y);
            if (rightRadius !== 0) {
                this.quadraticCurveTo(x + width, y, x + width, y + rightRadius);
            }

            this.lineTo(x + width, y + height - rightRadius);
            if (rightRadius !== 0){
                this.quadraticCurveTo(x + width, y + height, x + width - rightRadius, y + height);
            }

            this.lineTo(x + leftRadius, y + height);
            if (leftRadius !== 0){
                this.quadraticCurveTo(x, y + height, x, y + height - leftRadius);
            }

            this.lineTo(x, y + leftRadius);
            if (leftRadius !== 0){
                this.quadraticCurveTo(x, y, x + leftRadius, y);
            }

            this.closePath();
        };

        CanvasRenderingContext2D.prototype.angularArrowButton = function(x, y, w, h, radius, arrowWidth, arrowDirection, fill, stroke) {
            this.save();

            if (fill) {
                this.fillStyle = fill;
            }
            if (stroke) {
                this.strokeStyle = stroke;
                this.lineWidth = 1;
            }

            this.lineJoin = "round";
            this.beginPath();

            if (arrowDirection === "right") {
                this.moveTo(x + radius, y);
                this.lineTo(x + w - arrowWidth, y);
                this.lineTo(x + w, y + h / 2);
                this.lineTo(x + w - arrowWidth, y + h);
            }
            else {
                this.moveTo(x + arrowWidth, y);
                this.lineTo(x + w - radius, y);
                this.quadraticCurveTo(x + w, y, x + w, y + radius);
                this.lineTo(x + w, y + h - radius);
                this.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
            }

            if (arrowDirection === "right") {
                this.lineTo(x + radius, y + h);
                this.quadraticCurveTo(x, y + h, x, y + h - radius);
                this.lineTo(x, y + radius);
                this.quadraticCurveTo(x, y, x + radius, y);
            }
            else {
                this.lineTo(x + arrowWidth, y + h);
                this.lineTo(x, y + h / 2);
                this.lineTo(x + arrowWidth, y);
            }
            this.closePath();

            if (fill) {
                this.fill();
            }
            if (stroke) {
                this.stroke();
            }

            this.restore();
        };

        CanvasRenderingContext2D.prototype.circle = function(centerX, centerY, radius){
            this.beginPath();
            this.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
            this.closePath();
        };

        CanvasRenderingContext2D.prototype.strokeLine = function(x1, y1, x2, y2, style, width){
            this.save();
            this.beginPath();
            this.moveTo(x1, y1);
            this.lineTo(x2, y2);
            this.closePath();
            if(style !== undefined){
                this.strokeStyle = style;
            }
            if(width !== undefined){
                this.lineWidth = width;
            }
            this.stroke();
            this.restore();
        }
    }
})();